package com.example.znjt0617xmy;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private RelativeLayout relativeLayout;
    private TextView textView;
    private LinearLayout linearLayout;
    private EditText edtTxtMainUser;
    private EditText edtTxtMainPwd;
    private RelativeLayout relativeLayout2;
    private CheckBox chkPwd;
    private CheckBox chkLogin;
    private Button btnMainLogin;
    private Button btnMainRegister;
    private SharedPreferences sp;
    private Button btnRegister;

    @Override
    protected void onResume() {
        super.onResume();
        boolean isSavePwd = sp.getBoolean("isSavePwd", false);
        chkPwd.setChecked(isSavePwd);
        boolean isAutLogin = sp.getBoolean("isAutoLogin", false);
        chkLogin.setChecked(isAutLogin);
        String userName = sp.getString("userName", "");
        String userPwd = sp.getString("userPwd", "");
        if (isSavePwd) {
            edtTxtMainUser.setText(userName);
            edtTxtMainPwd.setText(userPwd);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        sp = getSharedPreferences("loginSetting", MODE_PRIVATE);
        chkPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean b) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("chkPwd", b);
                editor.commit();

            }
        });


        btnMainLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = edtTxtMainUser.getText().toString().trim();
                String userPwd = edtTxtMainPwd.getText().toString().trim();

            }
        });
        btnMainRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        boolean chkPwd = sp.getBoolean("chkPwd", false);
//        chkPwd.setChecked(chkPwd);
    }

    private void initView() {
        relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
        textView = (TextView) findViewById(R.id.textView);
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        edtTxtMainUser = (EditText) findViewById(R.id.edtTxt_main_user);
        edtTxtMainPwd = (EditText) findViewById(R.id.edtTxt_main_pwd);
        relativeLayout2 = (RelativeLayout) findViewById(R.id.relativeLayout2);
        chkPwd = (CheckBox) findViewById(R.id.chk_pwd);
        chkLogin = (CheckBox) findViewById(R.id.chk_login);
        btnMainLogin = (Button) findViewById(R.id.btn_main_login);
        btnMainRegister = (Button) findViewById(R.id.btn_register);
        btnRegister = (Button) findViewById(R.id.btn_register);
    }
}